package com.mediacloud.app.newsmodule.activity;

import android.os.Bundle;
import androidx.fragment.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import com.mediacloud.app.model.activity.BaseBackActivity4NoDetail;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.topic.TopicItemDetailFragment;
import com.mediacloud.app.newsmodule.utils.Addintegral;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig.ServerAppConfigInfo.NavigateBarStyle;

import static com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig.getAppServerConfigInfo;

import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.CatalogItem;

public class TopicItemDetailFragmentActivity extends BaseBackActivity4NoDetail {

	TopicItemDetailFragment fragment;
	ArticleItem articleItem;
	CatalogItem catalogItem;
	@Override
	protected boolean needLoadTopSecondBgSkin() 
	{
		//		return "1".equals(getResources().getString(R.string.need_load_second_topskin));
		return super.subClassgetNeedTopSecondBgSkin();
	}
	@Override
	protected NavigateBarStyle getNewsDetailNavigateBarStyle()
	{
		return getAppServerConfigInfo(this).getTopicNewsBar();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTitlebar_MoreContainer.setVisibility(View.VISIBLE);

		articleItem=getIntent().getParcelableExtra("data");
		catalogItem=getIntent().getParcelableExtra("catalog");
		catalogItem.setCatalogStyle(0);
		//		catalogItem.setCatid(String.valueOf(articleItem.getCid()));
		if(TextUtils.isEmpty(articleItem.getSummary()))
		{
			catalogItem.setCatname(articleItem.getTitle());
		}
		else
		{
			catalogItem.setCatname(articleItem.getSummary());
		}
		fragment=new TopicItemDetailFragment();
		Bundle bundle=new Bundle();
		bundle.putParcelable("catalog", catalogItem);
		bundle.putString("id", catalogItem.getCatid());
		bundle.putParcelable("topic", articleItem);
		fragment.setArguments(bundle); 
		FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.topicListContent, fragment);
		transaction.show(fragment);
		transaction.commitNowAllowingStateLoss();

		mTitlebar_More.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) 
			{
				fragment.showSharePanel(mRootView);
			}
		});
		Addintegral.addintegral(this,2,articleItem);
		setNewsTitle();
	}
	protected void setNewsTitle()
	{
		if(needLoadTopSecondBgSkin())
		{
			return;
		}
		if(!TextUtils.isEmpty(getServerConfigTitle()))
		{
			setTitle(getServerConfigTitle());
		}
		else if(isNavigateTitle()&&catalogItem!=null)
		{
			setTitle(catalogItem.getCatname());
		}
		else if(articleItem!=null)
		{
			setTitle(articleItem.getTitle());
		}
	}

	@Override
	public int getLayoutResID() {
		return R.layout.aappfactory_activity_fragment_topicdetail_detail;
	}

	@Override
	public void onBackPressed() {
		if(fragment.onBackPressed())
		{
			super.onBackPressed();
		}
	}
}